package lrscp.lib.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public abstract class SimpleTable<T extends SqlOperation<T>> implements Table<T> {
    private List<T> mLs = new ArrayList<T>();
    private Connection mCon;
    private PreparedStatement mSql;

    public SimpleTable(Connection connection, T obj) throws SQLException {
        mCon = connection;

        mSql = mCon.prepareStatement("select * from ?");
        mSql.setString(1, getTableName());
        ResultSet rs = mSql.executeQuery();
        while (rs.next()) {
            mLs.add(obj.fromSql(rs));
        }
    }

    @Override
    public abstract void add(T t);

    @Override
    public abstract void update(T t);

    @Override
    public abstract void remove(T t);

    @Override
    public List<T> getAll() {
        return mLs;
    }

    @Override
    public abstract String getTableName();
}
